package com.chookss.home.knowledge;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.base.StaticClass;
import com.chookss.home.NavigationBaseActivity;
import com.chookss.home.entity.NavigationEntity;
import com.chookss.tools.MyEvent;
import com.chookss.tools.Utils;
import com.chookss.video.VideoBaseActivity;
import com.johnrambo.ktea.ktExtends.ShareUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

public class NavigationKnowledgeAdapter extends BaseQuickAdapter<NavigationEntity, BaseViewHolderEx> {
    private Context mContext;
    private int index = 0;
    private int num = 0;
    private int type = 0;//0是正常模式展示 1代表第8个要展示更多
    private ArrayList<NavigationEntity> data;
    private String navigationCode1, navigationCode3;//底部code

    public NavigationKnowledgeAdapter(int layoutResId, Context context, @io.reactivex.annotations.Nullable ArrayList<NavigationEntity> data) {
        super(layoutResId, data);
        this.mContext = context;
        navigationCode1 = new ShareUtils().getString(mContext, StaticClass.BOTTOM_1_CODE, "13");
        navigationCode3 = new ShareUtils().getString(mContext, StaticClass.BOTTOM_3_CODE, "14");
    }

    @Override
    protected void convert(BaseViewHolderEx helper, NavigationEntity entity) {
        ImageView ivNavigation = helper.getView(R.id.ivNavigation);
        TextView tvNavigation = helper.getView(R.id.tvNavigation);
        TextView tvNavigationNum = helper.getView(R.id.tvNavigationNum);
        tvNavigation.setText(entity.getNavigationName());
        int num= Utils.changeNumber(entity.getNum());
        tvNavigationNum.setText(Utils.changeCounts2(entity.getNum()));

        Glide.with(mContext).load(entity.getLocalUrl()).into(ivNavigation);
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                01  公告管理 02  我的日志 03  资料库 04  学习计划
//                05  我的考试 06  一键面试 07  通讯录 08  个人能力
//                09 我的待办10 发布视频11 发布新闻12发布日志13题库14视频15溯源16发布图文17图文
                //141小视频 142视频 仅仅此处使用
                Intent intent = null;
                if ("03".equals(entity.getNavigationCode())) {
                    if (navigationCode1.equals("03")) {
                        EventBus.getDefault().post(new MyEvent("HomeBottom1"));
                    } else if (navigationCode3.equals("03")) {
                        EventBus.getDefault().post(new MyEvent("HomeBottom3"));
                    } else {
                        intent = new Intent(mContext, NavigationBaseActivity.class);
                        intent.putExtra("type", 3);
                        mContext.startActivity(intent);
                    }
                } else if ("13".equals(entity.getNavigationCode())) {
                    if (navigationCode1.equals("13")) {
                        EventBus.getDefault().post(new MyEvent("HomeBottom1"));
                    } else if (navigationCode3.equals("13")) {
                        EventBus.getDefault().post(new MyEvent("HomeBottom3"));
                    } else {
                        intent = new Intent(mContext, NavigationBaseActivity.class);
                        intent.putExtra("type", 0);
                        mContext.startActivity(intent);
                    }
                } else if ("19".equals(entity.getNavigationCode())) {
                    if (navigationCode1.equals("14") || navigationCode3.equals("14")||navigationCode1.equals("19") || navigationCode3.equals("19")) {
                        EventBus.getDefault().post(new MyEvent("HomeVedioBottom1"));
                        //定时器 倒计时
                        Observable.timer(400, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Consumer<Long>() {
                                    @Override
                                    public void accept(Long aLong) {
                                        try {
                                            EventBus.getDefault().post(new MyEvent("HomeVedio1"));
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                    } else {
                        intent = new Intent(mContext, VideoBaseActivity.class);
                        intent.putExtra("index",1);
                        mContext.startActivity(intent);
                    }

                } else if ("14".equals(entity.getNavigationCode())) {
                    if (navigationCode1.equals("14") || navigationCode3.equals("14")||navigationCode1.equals("19") || navigationCode3.equals("19")) {
                        EventBus.getDefault().post(new MyEvent("HomeVedioBottom2"));
                        EventBus.getDefault().post(new MyEvent("HomeVedio2"));
                    } else {
                        intent = new Intent(mContext, VideoBaseActivity.class);
                        mContext.startActivity(intent);
                    }
                } else if ("17".equals(entity.getNavigationCode())) {
                    if (navigationCode1.equals("17")) {
                        EventBus.getDefault().post(new MyEvent("HomeBottom1"));
                    } else if (navigationCode3.equals("17")) {
                        EventBus.getDefault().post(new MyEvent("HomeBottom3"));
                    } else {
                        intent = new Intent(mContext, NavigationBaseActivity.class);
                        intent.putExtra("type", 17);
                        mContext.startActivity(intent);
                    }
                } else if ("26".equals(entity.getNavigationCode())) {
                    if (navigationCode1.equals("26")) {
                        EventBus.getDefault().post(new MyEvent("HomeBottom1"));
                    } else if (navigationCode3.equals("26")) {
                        EventBus.getDefault().post(new MyEvent("HomeBottom3"));
                    } else {
                        intent = new Intent(mContext, NavigationBaseActivity.class);
                        intent.putExtra("type", 26);
                        mContext.startActivity(intent);
                    }
                }
            }
        });
    }

    public void setChoice(int num) {
        this.num = num;
        notifyItemChanged(index);
    }

    public void setType(int type) {
        this.type = type;
    }
}
